<?php
/**
 *
 * @author Strawberry🍓
 * @date 2021-05-21 4:08 下午
 *
 */

namespace app\common\model\tang;


use app\common\impl\Comm;
use app\common\model\CommonBaseModel;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

class SaleReport extends CommonBaseModel implements Comm
{

    public function getAdminPageData($param = [], $order = 'id asc')
    {
        $where = [];
        $model = self::where($where);
        if (!empty($param['Date'])){
            if(!empty($param['Date']['startDate'])){
                $model->where('report_time','>=',strtotime($param['Date']['startDate'])) ;
            }
            if(!empty($param['Date']['endDate'])){
                $model->where('report_time','<=',strtotime($param['Date']['endDate'].' 23:59:59'));
            }
        }
            if(!empty($param['startDate'])){
                $model->where('report_time','>=',strtotime($param['startDate'])) ;
            }
            if(!empty($param['endDate'])){
                $model->where('report_time','<=',strtotime($param['endDate'].' 23:59:59'));
        }
        if(isset($param['zubie']) &&!empty($param['zubie'])){
            $model->whereIn('merchant_id',$param['zubie']);
        }
        if(isset($param['merchant_id']) &&!empty($param['merchant_id'])){
            $model->whereIn('merchant_id',$param['merchant_id']);
        }
        $data = $model->with(['merchant'])->order($order)->select()->toArray();
        $data = $this->getList($data);
        if (!empty($param['export'])) {
            $this->export($data);
        }
        $count = count($data);
        //page??总页数
        $page = $param['page'];
        $limit = $param['limit'];
        $datas = array_splice($data, ($page - 1) * $limit, $limit);
        return parent::ajaxResult($datas, $count);
    }
    public function getList($data)
    {
        $datas = [];
        if ($data) {
            foreach ($data as $k => $v) {
                $arr = [];
                if(empty($v['merchant']['name'])||$v['merchant']['name']=='-'){
                    $v['merchant']['name'] = $v['merchant']['company'];
                }
                $arr['id'] = $v['id'];
                $arr['XH'] = $k + 1;
                $arr['PP'] =$v['merchant']['name'];
                $arr['LC'] =$v['merchant']['lcName'];
                $arr['PWH'] =$v['merchant']['berth_number'];
                $arr['YT'] = $v['merchant']['activities'];
                $arr['money'] = $v['money']/100;
                $arr['sale_number'] = $v['sale_number'];
                $arr['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
                $arr['report_time'] = date('Y-m-d',$v['report_time']);
                if($v['status']==0){
                    $status = '未审核';
                }
                if($v['status']==1){
                    $status = '审核通过';
                }
                if($v['status']==2){
                    $status = '驳回';
                }
                $arr['status'] =$status  ;
                $arr['is_auto'] =$v['is_auto']==1?'是':'否';
                $datas[] = $arr;
            }
        }
        return $datas;
    }
    private static $cellKey = array(
        'A', 'B', 'C', 'D', 'E', 'F','G','H','I','J','K'
    );
    public function export($data)
    {
        $name = '销售报表审核管理';
        $head = ['序号', '品牌', '楼层', '铺位号','业态', '金额', '笔数','上报时间','交易日期','状态','是否自动审核'];
        foreach ($data as $k=>$v) {
            unset($data[$k]['id']);
        }
        $styleArray = [
            'alignment' => [
                'horizontal' => 'center', //水平居中
                'vertical' => 'center', //垂直居中
            ],
            'font' => [
//                'name' => '黑体',
                'bold' => false,
//                'size' => 10
            ]];
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet()->setTitle('销售报表审核管理');
        //第一行
        $sheet->mergeCells('A1:K1', '');
        $spreadsheet->setActiveSheetIndex(0)->setCellValue('A1', '销售报表审核管理');
        $sheet->getStyle(1)->applyFromArray($styleArray);
        $spreadsheet->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        $spreadsheet->getActiveSheet()->getStyle('A1')->getFont()->setSize(20);
        $spreadsheet->getActiveSheet()->getRowDimension(1)->setRowHeight(30);
        $sheet->getStyle(2)->applyFromArray($styleArray);

        //第二行
        for ($i = 0; $i < count($head); $i++) {
            $spreadsheet->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//设置水平居中
            $spreadsheet->getActiveSheet()->getColumnDimension(self::$cellKey[$i])->setWidth(13);
            $sheet->setCellValue(self::$cellKey[$i] . '2', $head[$i]);
            $spreadsheet->getActiveSheet()->getStyle(self::$cellKey[$i] . '2')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);//设置垂直居中
            if($i==7||$i==8){
                $spreadsheet->getActiveSheet()->getColumnDimension(self::$cellKey[$i])->setWidth(20);
            }
            if($i==1){
                $spreadsheet->getActiveSheet()->getColumnDimension(self::$cellKey[$i])->setWidth(20);
            }
        }
        /*--------------开始从数据库提取信息插入Excel表中------------------*/
        $row =3;
        foreach ($data as $order){
            $clo = 1;
            foreach ($order as $val){
                $sheet->getStyle($row)->applyFromArray($styleArray);
                $spreadsheet->getActiveSheet()->getRowDimension($row)->setRowHeight(30);
                $spreadsheet->setActiveSheetIndex(0)->setCellValueByColumnAndRow($clo,$row,$val);
                $clo++;
            }
            $row++;
        }
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $name . '.xlsx"');
        header('Cache-Control: max-age=0');
        $objWriter = IOFactory::createWriter($spreadsheet, 'Xlsx');

        $objWriter->save('php://output');
        //删除清空：
        $spreadsheet->disconnectWorksheets();
        unset($spreadsheet);
        exit;
    }


    public function delById($id)
    {
        // TODO: Implement delById() method.
    }

    public function doAll($data)
    {
        // TODO: Implement doAll() method.
        return parent::doAllData($data);
    }
    public function merchant(): \think\model\relation\HasOne
    {
        return $this->hasOne(Merchant::class, 'id', 'merchant_id');
    }
}